package no.ntnu.fp.mmi;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import no.ntnu.fp.su.WatchWell;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
@SuppressWarnings("serial")
public class AddWellPanel extends javax.swing.JFrame{

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	private JTextField rValue;
	private JTextField pValue;
	private JTextField deltaP2limit;
	private JTextField deltaP1limit;
	private JTextField deltaPlimit;
	private JTextField deltaTlimit;
	private JTextField evLimit;
	private JTextField tValue;
	private JLabel rName;
	private JSeparator separator0;
	private JButton cancelButton;
	private JButton saveButton;
	private JSeparator seperator2;
	private JLabel P2name;
	private JLabel P1name;
	private JLabel Pname;
	private JLabel Tname;
	private JLabel EVname;
	private JLabel LIMname;
	private JLabel cValuesName;
	private JLabel tName;
	private JLabel pName;
	private JSeparator seperator;
	private JLabel addWell;
	Font bold = new Font("Candera", Font.BOLD, 12);
//	private int pID;
//	private int wID;
	private WatchModel model;
	private WatchPanel panel;
	
	
	/**
	 * Construct a new addwellpanel
	 * @param watchpanel sets panelmodel
	 * @param model sets model for watch
	 */
	
	public AddWellPanel(WatchPanel watchpanel, WatchModel model) {
		super();
		this.model = model;
		this.panel = watchpanel;
		
		
		try {	
			
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			setSize(350, 350);

		
			{
				Font font = new Font("Candera", Font.BOLD, 16);
				addWell = new JLabel();
				getContentPane().add(addWell);
				addWell.setText("Add Well");
				addWell.setBounds(27, 12, 77, 15);
				addWell.setFont(font);
				
			}
			{
				rValue = new JTextField();
				getContentPane().add(rValue);
				rValue.setBounds(82, 63, 70, 25);

			}
			{
				pValue = new JTextField();
				getContentPane().add(pValue); 
				pValue.setBounds(82, 91, 70, 25);
				

			}
			{
				tValue = new JTextField();
				getContentPane().add(tValue);
				tValue.setBounds(82, 120, 70, 25);
			}
			{
				seperator = new JSeparator();
				getContentPane().add(seperator);
				seperator.setBounds(32, 154, 261, 8);
			}
			{
				rName = new JLabel();
				getContentPane().add(rName);
				rName.setText("R");
				rName.setBounds(65, 65, 10, 15);
			}
			{
				pName = new JLabel();
				getContentPane().add(pName);
				pName.setText("P");
				pName.setBounds(65, 94, 10, 15);
			}
			{
				tName = new JLabel();
				getContentPane().add(tName);
				tName.setText("T");
				tName.setBounds(65, 123, 10, 15);
			}
			{
				cValuesName = new JLabel();
				getContentPane().add(cValuesName);
				cValuesName.setText("Critical Values");
				cValuesName.setBounds(43, 46, 90, 15);
				cValuesName.setFont(bold);
			}
			{
				LIMname = new JLabel();
				getContentPane().add(LIMname);
				LIMname.setText("Limits");
				LIMname.setBounds(38, 168, 50, 15);
				LIMname.setFont(bold);
			}
			{
				evLimit = new JTextField();
				getContentPane().add(evLimit);
				evLimit.setBounds(80, 183, 70, 23);
			}
			{
				deltaTlimit = new JTextField();
				getContentPane().add(deltaTlimit);
				deltaTlimit.setBounds(80, 208, 70, 23);
			}
			{
				deltaPlimit = new JTextField();
				getContentPane().add(deltaPlimit);
				deltaPlimit.setBounds(80, 235, 70, 23);
			}
			{
				EVname = new JLabel();
				getContentPane().add(EVname);
				EVname.setText("EV ");
				EVname.setBounds(62, 189, 18, 15);
			}
			{
				Tname = new JLabel();
				getContentPane().add(Tname);
				Tname.setText("\u0394T");
				Tname.setBounds(62, 214, 18, 15);
			}
			{
				Pname = new JLabel();
				getContentPane().add(Pname);
				Pname.setText("\u0394P");
				Pname.setBounds(62, 241, 18, 15);
			}
			{
				deltaP1limit = new JTextField();
				getContentPane().add(deltaP1limit);
				deltaP1limit.setBounds(212, 183, 70, 23);
			}
			{
				deltaP2limit = new JTextField();
				getContentPane().add(deltaP2limit);
				deltaP2limit.setBounds(212, 208, 70, 23);
			}
			{
				P1name = new JLabel();
				getContentPane().add(P1name);
				P1name.setText("\u0394P1");
				P1name.setBounds(176, 187, 24, 15);
			}
			{
				P2name = new JLabel();
				getContentPane().add(P2name);
				P2name.setText("\u0394P2");
				P2name.setBounds(176, 214, 24, 15);
			}
			{
				seperator2 = new JSeparator();
				getContentPane().add(seperator2);
				seperator2.setBounds(32, 271, 261, 10);
			}
			{
				saveButton = new JButton();
				getContentPane().add(saveButton);
				saveButton.setText("Add");
				saveButton.setBounds(32, 280, 86, 22);
				saveButton.addActionListener(new SaveButtonEvent());
			}
			{
				cancelButton = new JButton();
				getContentPane().add(cancelButton);
				cancelButton.setText("Cancel");
				cancelButton.setBounds(207, 280, 86, 22);
				cancelButton.addActionListener(new CancelButtonEvent());
			}
			{
				separator0 = new JSeparator();
				getContentPane().add(separator0);
				separator0.setBounds(27, 30, 67, 7);

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 *  Listener for cancel button, when pressed closes addwellpanel and does not add a well
	 
	 */
	public class CancelButtonEvent implements ActionListener {

		private Object[] option = {"Yes", "No"};
		public void actionPerformed(ActionEvent e) {
			int val = javax.swing.JOptionPane.showOptionDialog(null, "Are you sure you want to exit without saving?",
	                "Cancel?", javax.swing.JOptionPane.YES_NO_OPTION,
	                javax.swing.JOptionPane.QUESTION_MESSAGE, null, option, option[0]);
	        
	        if(val == 0) {
	            dispose();
	        }
			
		}		
	}
	/**
	 *  Listener for save button, when pressed closes addwellpanel and adds a well.
	 *  Checks if all textfields are set before adding
	 
	 */
	public class SaveButtonEvent implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			
			String rv = rValue.getText();
			String pv = pValue.getText();
			String dp2 = deltaP2limit.getText();
			String dp1 = deltaP1limit.getText();
			String dp = deltaPlimit.getText();
			String dt = deltaTlimit.getText();
			String ev = evLimit.getText();
			String tv = tValue.getText();
			int check = 1;
			
			if (rv.equals("") || pv.equals("") || dp2.equals("") || dp1.equals("") || 
				dp.equals("") || dt.equals("") || ev.equals("") || tv.equals("")){
				check = 0;	
			}
			
			if(check == 0){
				JOptionPane.showMessageDialog(null	, "You did not fill all the fields", "Error", JOptionPane.INFORMATION_MESSAGE);
			}
			else {
				double doRv =Double.parseDouble(rv);
				double doPv =Double.parseDouble(pv);
				double doDp2 =Double.parseDouble(dp2);
				double doDp1 =Double.parseDouble(dp1);
				double doDp =Double.parseDouble(dp);
				double doDt =Double.parseDouble(dt);
				double doEv =Double.parseDouble(ev);
				double doTv =Double.parseDouble(tv);
				
				WatchWell w = model.getWatch().getPlatform().newWell(doRv, doPv, doTv, doEv, doDt, doDp1, doDp2, doDp);
				w.startPoll();
				model.setWellList(model.getWatch().getPlatform().getWells());
				panel.populateWellList();
			
			JOptionPane.showMessageDialog(null	, "Well Added Sucsessfully", "Well Added", JOptionPane.INFORMATION_MESSAGE);

			
			dispose();
			}
			
		}		
	}
	


}
